package com.itcanteen.medical.patient.util;

import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.Properties;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

/**
 * 发短信 工具类
 * @author 1804A-TeamOne
 * @date 2020-12-17 19:14
 */
public class SendMessageServiceUtil {
    /**
     * 发送邮件账号
     */
    private static final String MY_EMAIL_ACCOUT = "1752140@qq.com";
    /**
     * 发送邮箱账号的密码
     */
    private static final String MY_EMAIL_PASSWORD = "qkxjetizsdpicadi";

    /**
     * 使用qq邮箱格式
     */
    private static final String MY_EMAIL_SMTP_HOST = "smtp.qq.com";
    /**
     * 用户邮箱
     */
    private static String receiveMailAccount = "";

    /**
     * 控制层调用方法
     * @param mail 邮箱
     * @param code 验证码
     * @throws UnsupportedEncodingException 不支持编码格式异常
     * @throws MessagingException 消息异常
     */
    public static void sendMessage(String mail, int code) throws UnsupportedEncodingException, MessagingException {
        //收件人邮箱账号
        receiveMailAccount = mail;
        //要发送的验证码
        send(code);
    }

    public static void send(int code) throws MessagingException, UnsupportedEncodingException {
        Properties properties = new Properties();
        // 使用的协议（JavaMail规范要求）
        properties.setProperty("mail.transport.protocol", "smtp");
        // 发件人的邮箱的 SMTP 服务器地址
        properties.setProperty("mail.smtp.host", MY_EMAIL_SMTP_HOST);
        // 需要请求认证
        properties.setProperty("mail.smtp.auth", "true");
        final String smtpPort = "465";
        properties.setProperty("mail.smtp.port", smtpPort);
        properties.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
        properties.setProperty("mail.smtp.socketFactory.fallback", "false");
        properties.setProperty("mail.smtp.socketFactory.port", smtpPort);
        Session instance = Session.getInstance(properties);
        instance.setDebug(true);
        MimeMessage message = createMimeMessage(instance, MY_EMAIL_ACCOUT, receiveMailAccount,code);
        Transport transport = instance.getTransport();
        transport.connect(MY_EMAIL_ACCOUT, MY_EMAIL_PASSWORD);
        transport.sendMessage(message, message.getAllRecipients());
        transport.close();
    }

    public static MimeMessage createMimeMessage(Session instance, String sendMail, String receiveMail,int code) throws UnsupportedEncodingException, MessagingException {
//int var1=var;
        //  创建一封邮件
        MimeMessage message = new MimeMessage(instance);

        //  From: 发件人（昵称有广告嫌疑，避免被邮件服务器误认为是滥发广告以至返回失败，请修改昵称）
        message.setFrom(new InternetAddress(sendMail, "您的验证码", "UTF-8"));

        //  To: 收件人（可以增加多个收件人、抄送、密送）
        message.setRecipient(MimeMessage.RecipientType.TO, new InternetAddress(receiveMail, "用户", "UTF-8"));
        //  Subject: 邮件主题（标题有广告嫌疑，避免被邮件服务器误认为是滥发广告以至返回失败，请修改标题）
        message.setSubject("验证码", "UTF-8");
        //  Content: 邮件正文（可以使用html标签)
        //将随机生成的验证码与写好的模板结合
        message.setContent("    尊敬的用户您好,这是您的验证码："+code+",如果不是本人操作请忽略。", "text/html;charset=UTF-8");
        //  设置发件时间
        message.setSentDate(new Date());
        //  保存设置
        message.saveChanges();
        return message;
    }

}
